package com.lgc.javathread.threadWaitTest.waitNotify;

//打印10次A，B线程打印10次B
public class MyThreadPrinter1 implements Runnable {
    private String name;
    private Object prev;
    private Object self;

    private MyThreadPrinter1(String name, Object prev, Object self) {
        this.name = name;
        this.prev = prev;
        this.self = self;
    }

    @Override
    public void run() {
        int count=10;
        while (count>0){
            synchronized (prev){
                synchronized (self){
                    System.out.println(name);
                    count--;

                    self.notify();
                }

                try {
                    prev.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Object a=new Object();
        Object b = new Object();

        MyThreadPrinter1 printA = new MyThreadPrinter1("A",b,a);
        MyThreadPrinter1 printB = new MyThreadPrinter1("B",a,b);

        new Thread(printA).start();
        Thread.sleep(100);
        new Thread(printB).start();
    }


}
